//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "RiskyAssetSwap.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CRiskyAssetSwap::CRiskyAssetSwap (Boolean fixedPayer, Double nominal, Cephei::QL::Times::ISchedule^ fixedSchedule, Cephei::QL::Times::ISchedule^ floatSchedule, Cephei::QL::Times::IDayCounter^ fixedDayCounter, Cephei::QL::Times::IDayCounter^ floatDayCounter, Double spread, Double recoveryRate_, Cephei::QL::Termstructures::IYieldTermStructure^ yieldTS, Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ defaultTS, Microsoft::FSharp::Core::FSharpOption<Double>^ coupon, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CRiskyAssetSwap::typeid)
{
    CSchedule^ _CfixedSchedule;
    CSchedule^ _CfloatSchedule;
    CDayCounter^ _CfixedDayCounter;
    CDayCounter^ _CfloatDayCounter;
    CYieldTermStructure^ _CyieldTS;
    CDefaultProbabilityTermStructure^ _CdefaultTS;
    try
    {
#ifdef HANDLE
        _phRiskyAssetSwap = NULL;
#endif
        bool _fixedPayer = (bool)ValueHelper::Convert (fixedPayer); //d
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal); //d
        _CfixedSchedule = safe_cast<CSchedule^> (fixedSchedule);
        _CfixedSchedule->Lock();
        QuantLib::Schedule& _fixedSchedule = static_cast<QuantLib::Schedule&> (_CfixedSchedule->GetReference ()); 
        _CfloatSchedule = safe_cast<CSchedule^> (floatSchedule);
        _CfloatSchedule->Lock();
        QuantLib::Schedule& _floatSchedule = static_cast<QuantLib::Schedule&> (_CfloatSchedule->GetReference ()); 
        _CfixedDayCounter = safe_cast<CDayCounter^> (fixedDayCounter);
        _CfixedDayCounter->Lock();
        QuantLib::DayCounter& _fixedDayCounter = static_cast<QuantLib::DayCounter&> (_CfixedDayCounter->GetReference ()); 
        _CfloatDayCounter = safe_cast<CDayCounter^> (floatDayCounter);
        _CfloatDayCounter->Lock();
        QuantLib::DayCounter& _floatDayCounter = static_cast<QuantLib::DayCounter&> (_CfloatDayCounter->GetReference ()); 
        QuantLib::Rate _spread = (QuantLib::Rate)ValueHelper::Convert (spread); //d
        QuantLib::Rate _recoveryRate_ = (QuantLib::Rate)ValueHelper::Convert (recoveryRate_); //d
        _CyieldTS = safe_cast<CYieldTermStructure^> (yieldTS);
        _CyieldTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _yieldTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CyieldTS->GetHandle ()); 
        _CdefaultTS = safe_cast<CDefaultProbabilityTermStructure^> (defaultTS);
        _CdefaultTS->Lock();
        Handle<QuantLib::DefaultProbabilityTermStructure>& _defaultTS = static_cast<Handle<QuantLib::DefaultProbabilityTermStructure>&> (_CdefaultTS->GetHandle ()); 
        QuantLib::Rate _coupon = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (coupon) ? (QuantLib::Rate)ValueHelper::Convert (coupon->Value) : Null<QuantLib::Rate>()); //4
        _ppRiskyAssetSwap = new boost::shared_ptr<QuantLib::RiskyAssetSwap> (new QuantLib::RiskyAssetSwap ( _fixedPayer,  _nominal,  _fixedSchedule,  _floatSchedule,  _fixedDayCounter,  _floatDayCounter,  _spread,  _recoveryRate_,  _yieldTS,  _defaultTS,  _coupon ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppRiskyAssetSwap)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwap));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfixedSchedule != nullptr) _CfixedSchedule->Unlock();
        if (_CfloatSchedule != nullptr) _CfloatSchedule->Unlock();
        if (_CfixedDayCounter != nullptr) _CfixedDayCounter->Unlock();
        if (_CfloatDayCounter != nullptr) _CfloatDayCounter->Unlock();
        if (_CyieldTS != nullptr) _CyieldTS->Unlock();
        if (_CdefaultTS != nullptr) _CdefaultTS->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwap::CRiskyAssetSwap (boost::shared_ptr<QuantLib::RiskyAssetSwap>& childNative, Object^ owner) : CInstrument(CRiskyAssetSwap::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwap = NULL;
#endif
	_ppRiskyAssetSwap = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwap));
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwap::CRiskyAssetSwap (QuantLib::RiskyAssetSwap& childNative, Object^ owner) : CInstrument(CRiskyAssetSwap::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwap = NULL;
#endif
	_ppRiskyAssetSwap = new boost::shared_ptr<QuantLib::RiskyAssetSwap> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwap));
    _RiskyAssetSwapOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Experimental::Credit::CRiskyAssetSwap::CRiskyAssetSwap (CRiskyAssetSwap^ copy) : CInstrument(CRiskyAssetSwap::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwap = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRiskyAssetSwap = new boost::shared_ptr<QuantLib::RiskyAssetSwap> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwap));
    }
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwap::CRiskyAssetSwap (PLATFORM::Type^ t) : CInstrument(CRiskyAssetSwap::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwap = NULL;
#endif
	if (!t->IsSubclassOf(CRiskyAssetSwap::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CRiskyAssetSwap::CRiskyAssetSwap (QuantLib::Handle<QuantLib::RiskyAssetSwap>& childNative, Object^ owner)  : CInstrument(CRiskyAssetSwap::typeid)
{
	_phRiskyAssetSwap = &childNative;
	_ppRiskyAssetSwap = &static_cast<boost::shared_ptr<QuantLib::RiskyAssetSwap>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwap));
    _RiskyAssetSwapOwner = owner;
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwap::CRiskyAssetSwap (QuantLib::Handle<QuantLib::RiskyAssetSwap> childNative)  : CInstrument(CRiskyAssetSwap::typeid)
{
	_phRiskyAssetSwap = &childNative;
	_ppRiskyAssetSwap = &static_cast<boost::shared_ptr<QuantLib::RiskyAssetSwap>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwap));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CRiskyAssetSwap::CRiskyAssetSwap (QuantLib::RiskyAssetSwap childNative)  : CInstrument(CRiskyAssetSwap::typeid)
{
#ifdef HANDLE
	_phRiskyAssetSwap = NULL;
#endif
	_ppRiskyAssetSwap = new boost::shared_ptr<QuantLib::RiskyAssetSwap> (new QuantLib::RiskyAssetSwap (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppRiskyAssetSwap));
}
#endif

Cephei::QL::Experimental::Credit::CRiskyAssetSwap::~CRiskyAssetSwap ()
{
    if (_ppRiskyAssetSwap != NULL)
    {
	    delete _ppRiskyAssetSwap;
        _ppRiskyAssetSwap = NULL;
    }
}
Cephei::QL::Experimental::Credit::CRiskyAssetSwap::!CRiskyAssetSwap ()
{
    if (_ppRiskyAssetSwap != NULL)
    {
	    delete _ppRiskyAssetSwap;
    }
}
QuantLib::RiskyAssetSwap& Cephei::QL::Experimental::Credit::CRiskyAssetSwap::GetReference ()
{
    if (_ppRiskyAssetSwap == NULL) throw REFNEW NativeNullException ();
	return **_ppRiskyAssetSwap;
}
boost::shared_ptr<QuantLib::RiskyAssetSwap>& Cephei::QL::Experimental::Credit::CRiskyAssetSwap::GetShared ()
{
    if (_ppRiskyAssetSwap == NULL) throw REFNEW NativeNullException ();
	return *_ppRiskyAssetSwap;
}
QuantLib::RiskyAssetSwap* Cephei::QL::Experimental::Credit::CRiskyAssetSwap::GetPointer ()
{
    if (_ppRiskyAssetSwap == NULL) throw REFNEW NativeNullException ();
	return &**_ppRiskyAssetSwap;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RiskyAssetSwap>& Cephei::QL::Experimental::Credit::CRiskyAssetSwap::GetHandle ()
{
	if (_phRiskyAssetSwap == NULL)
	{
		_phRiskyAssetSwap = new Handle<QuantLib::RiskyAssetSwap> (*_ppRiskyAssetSwap);
	}
	return *_phRiskyAssetSwap;
}
#endif
bool Cephei::QL::Experimental::Credit::CRiskyAssetSwap::HasNative () 
{
	return (_ppRiskyAssetSwap != NULL);
}

Double Cephei::QL::Experimental::Credit::CRiskyAssetSwap::FairSpread::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRiskyAssetSwap)->fairSpread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Credit::CRiskyAssetSwap::FixedPayer::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	bool _rv = (bool)(*_ppRiskyAssetSwap)->fixedPayer ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CRiskyAssetSwap::FloatAnnuity::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRiskyAssetSwap)->floatAnnuity ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CRiskyAssetSwap::Nominal::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppRiskyAssetSwap)->nominal ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CRiskyAssetSwap::Spread::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppRiskyAssetSwap)->spread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IRiskyAssetSwap^ Cephei::QL::Experimental::Credit::CRiskyAssetSwap_Factory::Create (Boolean fixedPayer, Double nominal, Cephei::QL::Times::ISchedule^ fixedSchedule, Cephei::QL::Times::ISchedule^ floatSchedule, Cephei::QL::Times::IDayCounter^ fixedDayCounter, Cephei::QL::Times::IDayCounter^ floatDayCounter, Double spread, Double recoveryRate_, Cephei::QL::Termstructures::IYieldTermStructure^ yieldTS, Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ defaultTS, Microsoft::FSharp::Core::FSharpOption<Double>^ coupon, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CRiskyAssetSwap ( fixedPayer,  nominal,  fixedSchedule,  floatSchedule,  fixedDayCounter,  floatDayCounter,  spread,  recoveryRate_,  yieldTS,  defaultTS,  coupon,  QL_Pricer);
}
